package day15;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

/**
 * @author oldliu
 * @since 1.0
 */
public class XMLOp {
    static Document doc;

    static {
        System.out.println("sssssssss");
        //doc=...
    }

    public static void main(String[] args) {
        //readALL();
        //add(2,"java oop","9","java oop的基本点");
        //modify(2, "java面向对象", "Java最核心的部分！");
        //readItem("java 基础");
        del(3);
    }

    //
    //根据name修改content和diff
    public static void modify(String name, String content, int diff) {
        SAXReader reader = new SAXReader();
        Document doc = null;
        try {
            doc = reader.read(XMLOp.class.getClassLoader().getResourceAsStream("day15/books.xml"));
        } catch (DocumentException e) {
            e.printStackTrace();
            return;
        }
        Element e = (Element) doc.selectSingleNode("/books/book[name='" + name + "']");
        if (e == null) {
            System.out.println("meiyou " + name);
            return;
        }

        e.selectSingleNode("content").setText(content);
        try {
            OutputFormat format = OutputFormat.createPrettyPrint();
            format.setEncoding("utf-8");
            XMLWriter xw = new XMLWriter(new FileWriter("java高级/src/day15/books.xml"), format);
            xw.write(doc);
            xw.close();
        } catch (IOException ee) {
            ee.printStackTrace();
        }
    }

    //根据item修改name和content
    public static void modify(int item, String content, String newName) {
        SAXReader reader = new SAXReader();
        Document doc = null;
        try {
            doc = reader.read(XMLOp.class.getClassLoader().getResourceAsStream("day15/books.xml"));
        } catch (DocumentException e) {
            e.printStackTrace();
            return;
        }
        List list = doc.selectNodes("/books/book[@item=" + item + "]");
        if (list.size() == 0) {
            System.out.println("meiyou " + item);
            return;
        }
        Element e = (Element) list.get(0);
        e.selectSingleNode("name").setText(newName);
        e.selectSingleNode("content").setText(content);
        try {
            OutputFormat format = OutputFormat.createPrettyPrint();
            format.setEncoding("utf-8");
            XMLWriter xw = new XMLWriter(new FileWriter("java高级/src/day15/books.xml"), format);
            xw.write(doc);
            xw.close();
        } catch (IOException ee) {
            ee.printStackTrace();
        }
    }

    public static void add(int item, String name, String diff, String content) {
        SAXReader reader = new SAXReader();
        Document doc = null;
        try {
            doc = reader.read(XMLOp.class.getClassLoader().getResourceAsStream("day15/books.xml"));
        } catch (DocumentException e) {
            e.printStackTrace();
            return;
        }
        Element root = doc.getRootElement();
        Element book = root.addElement("book");
        book.addAttribute("item", String.valueOf(item));
        book.addElement("name").setText(name);
        book.addElement("difficulty").setText(diff);
        book.addElement("content").setText(content);
        //保存回去
        try {
            OutputFormat format = OutputFormat.createPrettyPrint();
            format.setEncoding("utf-8");
            XMLWriter xw = new XMLWriter(new FileWriter("java高级/src/day15/books.xml"), format);
            xw.write(doc);
            xw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static void del(int item) {
        SAXReader reader = new SAXReader();
        Document doc = null;
        try {
            doc = reader.read(XMLOp.class.getClassLoader().getResourceAsStream("day15/books.xml"));
        } catch (DocumentException e) {
            e.printStackTrace();
            return;
        }
        Element e = (Element)doc.selectSingleNode("/books/book[@item='" + item + "']");
        if(e==null){
            System.out.println("没有找到"+item+"的课程");
            return;
        }
        e.getParent().remove(e);//从他的上级节点中删除自己
        //保存回去
        try {
            OutputFormat format = OutputFormat.createPrettyPrint();
            format.setEncoding("utf-8");
            XMLWriter xw = new XMLWriter(new FileWriter("java高级/src/day15/books.xml"), format);
            xw.write(doc);
            xw.close();
        } catch (IOException ee) {
            ee.printStackTrace();
        }
    }

    public static void readALL() {
        SAXReader reader = new SAXReader();
        Document doc = null;
        try {
            doc = reader.read(XMLOp.class.getClassLoader().getResourceAsStream("day15/books.xml"));
        } catch (DocumentException e) {
            e.printStackTrace();
            return;
        }
        Element root = doc.getRootElement();
        System.out.println("根节点名称：" + root.getName() + ",value=" + root.getText().trim());
        //rootElement.elements();
        List<Element> list = doc.selectNodes("/books/book");
        for (Element e : list) {
            int item = Integer.parseInt(e.attributeValue("item"));
            Element ename = (Element) e.selectSingleNode("name");//(Element) e.selectNodes("name").get(0);
            String name = ename.getText();
            Element edif = (Element) e.selectSingleNode("difficulty");
            String diff = edif.getText();
            //Element econtent = (Element) e.selectSingleNode("content");
            String content = e.elementTextTrim("content");
            System.out.printf("item=%d,name=%s,diff=%s,content:%s", item, name, diff, content);
        }
    }

    public static void readItem(String name) {
        SAXReader reader = new SAXReader();
        Document doc = null;
        try {
            doc = reader.read(XMLOp.class.getClassLoader().getResourceAsStream("day15/books.xml"));
        } catch (DocumentException e) {
            e.printStackTrace();
            return;
        }

        List<Element> list = doc.selectNodes("/books/book/name[text()='" + name + "']");
        System.out.println(list.size());

    }
}
